<template>
  <div>
    <el-card class="box-card">
      <span><i class="iconfont icon-dingdan1 icon"> </i>填写商品分类信息</span>
    </el-card>
    <el-form
      ref="productValue"
      :rules="rules"
      :model="productValue"
      label-width="200px"
    >
      <el-form-item label="分类名称:" prop="name">
        <el-input v-model="productValue.name" class="input-width"></el-input>
      </el-form-item>
      <el-form-item label="上级分类:">
        <el-select
          v-model="productValue.parent_id"
          placeholder="请选择"
          class="input-width"
        >
          <el-option
            v-for="item in productCateOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="数量单位:">
        <el-input
          v-model="productValue.product_unit"
          class="input-width"
        ></el-input>
      </el-form-item>
      <el-form-item label="排序:">
        <el-input v-model="productValue.sort" class="input-width"></el-input>
      </el-form-item>
      <el-form-item label="是否显示:">
        <el-radio-group v-model="productValue.show_status">
          <el-radio :label="1">是</el-radio>
          <el-radio :label="0">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="是否显示在导航栏:">
        <el-radio-group v-model="productValue.nav_status">
          <el-radio :label="1">是</el-radio>
          <el-radio :label="0">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="关键词:">
        <el-input
          v-model="productValue.keywords"
          class="input-width"
        ></el-input>
      </el-form-item>
      <el-form-item label="分类描述:">
        <el-input
          v-model="productValue.description"
          class="input-width"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">立即创建</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
//引入 api接口
// parent_id = 0 表示的是顶级分类
import { fetchProductCatePage } from "@/api/product"; //addProductCate ,
export default {
  name: "productCateAdd",
  data: function () {
    return {
      productValue: {
        description: "", //描述
        icon: "", //小图标
        keywords: "", //关键词
        name: "", //名称
        nav_status: 0, //是否显示在导航
        parent_id: 0, //上级节点id
        product_unit: "", //数量单位
        product_count: 0, //商品数量
        show_status: 0, //是否显示
        sort: 0, //排序
        level: 0, //0表示一级分类 1表示二级分类},
      },
      rules: {
        name: [{ required: true, message: "请输入分类名称", trigger: "blur" }],
      },
      productCateOptions: [], //分类的下拉列表数据
    };
  },
  methods: {
    onSubmit() {
      console.log("表单提交了");
      //添加分类的需要验证 ，再调用 后台接口
    },
    async getProductCatePage() {
      var res = await fetchProductCatePage({}, 0);
      console.log(res);
      //重组数据
      this.productCateOptions.push({ value: 0, label: "顶级分类" });
      res.data.forEach((item) => {
        this.productCateOptions.push({
          value: item.id,
          label: item.name,
        });
      });
    },
  },
  mounted() {
    this.getProductCatePage();
  },
};
</script>

<style>
</style>